<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="费用名称:">
        <el-input v-model="queryParams.frtName" size="small" placeholder="请输入费用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="dataList" highlight-current-row>
      <el-table-column label="客户名称" align="center" prop="customerName" width="180" />
      <el-table-column label="费用编码" align="center" prop="frtNo" width="180" />
      <el-table-column label="结算基于" align="center" prop="settleBy" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.settleBy" size="small" style="width: 100%;" clearable>
              <el-option v-for="option in $constant.settleByOptions" :key="option.key" :label="option.label" :value="option.key" />
            </el-select>
          </template>
          <span v-else>{{ row.settleBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额/百分比" align="center" prop="amount" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.amount" type="number" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期到" align="center" prop="validUntil" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-date-picker v-model="row.validUntil" type="date" size="small" value-format="yyyy-MM-dd" style="width: 100%" placeholder="选择有效期到" clearable />
          </template>
          <span v-else>{{ parseTime(row.validUntil, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同" align="center" prop="frtName" width="180">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-select v-model="row.contractNo" style="width: 100%;" clearable>
              <el-option v-for="option in options.contractOptions" :key="option.id" :label="option.skuName" :value="option.id" />
            </el-select>
          </template>
          <span v-else>{{ row.materialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed>
        <template slot-scope="{row}">
          <el-button v-if="row.edit" type="success" size="small" icon="el-icon-circle-check-outline" @click="handleEdit(row)">确认</el-button>
          <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="row.edit=!row.edit">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import ChargeConfigApi from '@/api/pms/ChargeConfigApi'
import ConContractApi from '@/api/contract/ConContractApi'

const defaultChargeConfigFormData = {
  customerId: null,
  customerName: null,
  frtNo: '包装费',
  frtName: null,
  settleBy: null,
  chargeTimeBy: null,
  amount: null,
  chargeType: null,
  customerType: null,
  inAndOutCharge: null,
  validUntil: null,
  convertQuantity: null,
  startQuantity: null,
  endQuantity: null,
  startDateFrom: null
}
export default {
  name: 'ChargeConfigList',
  props: {
    customer: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  watch: {
    customer() {
      this.handleQuery()
    }
  },
  data() {
    return {
      options: {
        contractOptions: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        frtNo: '包装费',
        frtName: null,
        isValid: this.$constant.Y
      },
      dataList: [],
      loading: true,
      searchVisible: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      const params = {
        customerNo: this.customer.id
      }
      ConContractApi.list(params).then(response => {
        this.options.contractOptions = response.data
      })
    },
    handleAdd() {
      console.log(this.customer)
      this.dataList.push({
        ...defaultChargeConfigFormData,
        customerId: this.customer.id,
        customerName: this.customer.customerName,
        edit: true
      })
    },
    handleRemove(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.remove(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      if (row.id) {
        ChargeConfigApi.edit(row).then(response => {
          this.$message.success('修改成功')
          this.handleQuery()
        })
      } else {
        ChargeConfigApi.add(row).then(response => {
          this.$message.success('添加成功')
          this.handleQuery()
        })
      }
    },
    handleQuery() {
      this.loading = true
      if (this.customer.id) {
        const params = {
          ...this.queryParams,
          customerId: this.customer.id
        }
        ChargeConfigApi.list(params).then(response => {
          this.dataList = response.data.map(item => {
            return {
              ...item,
              edit: false
            }
          })
          this.loading = false
        }, () => {
          this.loading = false
        })
      }
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ChargeConfigApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
